from urllib import request
from flask import jsonify


def loginManage(username, password):
    from myapp.entities.User import Userinfo
    from myapp.utils.CommonResponse import CommonResponse
    from myapp.utils.FJWT import createJWT

    print(username, password)
    searchUser = Userinfo.query.filter_by(username=username, password=password).first()
    if searchUser is None:
        return CommonResponse.requestFailureStatusAndMessage(401, "用户名或密码错误")
    else:
        userid = searchUser.userid
        access_token = createJWT(userid)
        # 返回用户ID和JWT令牌
        return jsonify({'userid': userid, 'jwt': access_token}), 200


def RegisterByEmail(register_info):
    from myapp.entities.User import Userinfo
    from myapp.exts  import db
    from myapp.utils.FJWT import createJWT
    from myapp.utils.CommonResponse import CommonResponse
    username = register_info.get("username")
    password = register_info.get("password")
    birthday = register_info.get("birthday")
    job = register_info.get("job")
    email = register_info.get("email")
    new_user = Userinfo(username=username, password=password, birthday=birthday, job=job, email=email)
    try:
        db.session.add(new_user)
        db.session.commit()
        userid = new_user.userid
        access_token = createJWT(userid)
        return jsonify({'userid': userid, 'jwt': access_token}), 200
    except Exception as e:
        print(e.with_traceback(None))
        db.session.rollback()
        return CommonResponse.requestFailureStatusAndMessage(401, "注册失败" + str(e))
    finally:
        db.session.close()


def RegisterByPhoneNum(register_info):
    from myapp.entities.User import Userinfo
    from myapp.exts  import db
    from myapp.utils.FJWT import createJWT
    from myapp.utils.CommonResponse import CommonResponse
    username = register_info.get("username")
    password = register_info.get("password")
    birthday = register_info.get("birthday")
    job = register_info.get("job")
    phoneNumber = register_info.get("phoneNumber")
    new_user = Userinfo(username=username, password=password, birthday=birthday, job=job, phoneNum=phoneNumber)
    try:
        db.session.add(new_user)
        db.session.commit()
        userid = new_user.userid
        access_token = createJWT(userid)
        return jsonify({'userid': userid, 'jwt': access_token}), 200
    except Exception as e:
        print(e.with_traceback(None))
        db.session.rollback()
        return CommonResponse.requestFailureStatusAndMessage(401, "注册失败" + str(e))
    finally:
        db.session.close()


def EditUserInformation(register_info):
    from myapp.entities.User import Userinfo
    from myapp.exts  import db
    from myapp.utils.FJWT import createJWT
    from myapp.utils.CommonResponse import CommonResponse
    username = register_info.get("username")
    password = register_info.get("password")
    birthday = register_info.get("birthday")
    job = register_info.get("job")
    phoneNumber = register_info.get("phoneNumber")
    email = register_info.get("email")
    userid = register_info.get("userid")
    searchUser = Userinfo.query.filter_by(userid=userid).first()
    if searchUser is None:
        return CommonResponse.requestFailureStatusAndMessage(401, "未查询到用户信息")
        # 更新用户信息
    if username is not None and username != "":
        searchUser.username = username
    if password is not None and password != "":
        searchUser.password = password
    if birthday is not None and birthday != "":
        searchUser.birthday = birthday
    if job is not None and job != "":
        searchUser.job = job
    if phoneNumber is not None and phoneNumber != "":
        searchUser.phoneNum = phoneNumber
    if email is not None and email != "":
        searchUser.email = email
    try:
        db.session.commit()
        return CommonResponse.requestFailureStatusAndMessage(200, "修改成功")
    except Exception as e:
        print(e.with_traceback(None))
        db.session.rollback()
        return CommonResponse.requestFailureStatusAndMessage(401, "修改失败" + str(e))
    finally:
        db.session.close()


def GetUserInformation(user_info):
    from myapp.entities.User import Userinfo
    from myapp.exts  import db
    from myapp.utils.FJWT import createJWT
    from myapp.utils.CommonResponse import CommonResponse
    userid = user_info.get("userid")
    print(userid)
    searchUser = Userinfo.query.filter_by(userid=userid).first()
    if searchUser is None:
        return CommonResponse.requestFailureStatusAndMessage(401, "未查询到用户信息")
    user_data = {
        "username": searchUser.username,
        "password": searchUser.password,
        "birthday": searchUser.birthday,
        "job": searchUser.job,
        "phoneNum": searchUser.phoneNum,
        "email": searchUser.email
    }
    return jsonify(user_data), 200